const Controller = require('egg').Controller;

class callHistoryController extends Controller {
  async getHistoryList() {
    const { ctx } = this;
    const param = ctx.query;
    const { page, size } = ctx.helper.setDefaultPageAndSize(
      param.page,
      param.size,
    );
    param.page = page;
    param.size = size;
    param.account_id = ctx.state.user.id
    const { data, error } = await ctx.service.callHistory.getHistoryList(param)
    const resBody = {
      code: 200,
      msg: 'Get call history list error',
      data
    }
    if (error) {
      resBody.code = -1;
      resBody.msg = error
    }

    ctx.body = resBody
  }

  async addHistory() {
    const { ctx } = this;
    ctx.validate({
      stu_id: { type:'number' },
      stu_name: { type: 'string' },
      class_name: { type: 'string' },
      is_absent: { type: 'number' }
    })
    const params = ctx.request.body
    params.account_id = ctx.state.user.id
    const { error } = await ctx.service.callHistory.addHistory(params)
    const resBody = {
      code: 200,
      msg: 'Add classes success',
    };
    if (error) {
      resBody.code = -1;
      resBody.msg = error
    }

    ctx.body = resBody;
  }

  async updateHistory() {
    const { ctx } = this;
    ctx.validate({
      history_id: { type: 'number' },
      is_absent: { type: 'number' }
    })
    const param = ctx.request.body;
    const { error } = await ctx.service.callHistory.updateHistory(param);
    const response = {
      code: 200,
      msg: 'Update call history success',
    };
    if (error) {
      response.code = -1;
      response.msg = error;
    }
    ctx.body = response;
  }

async delHistory() {
    const { ctx } = this;
    ctx.validate({
      history_id_list: { type: 'array' }
    })
    const param = ctx.request.body;
    const { error } = await ctx.service.callHistory.delHistory(param);
    const response = {
      code: 200,
      msg: 'Delete call history success',
    };
    if (error) {
      response.code = -1;
      response.msg = error;
    }
    ctx.body = response;
  }

  async delAllHistory() {
    const { ctx } = this;
    const param = { account_id: ctx.state.user.id }
    const { error } = await ctx.service.callHistory.delAllHistory(param);
    const response = {
      code: 200,
      msg: 'Delete call history success',
    };
    if (error) {
      response.code = -1;
      response.msg = error;
    }
    ctx.body = response;
  }
}

module.exports = callHistoryController;
